package com.horizon.mobile.activity;

import java.util.ArrayList;

import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.interfaces.OnBottomDragListener;
import zuo.biao.library.manager.HttpManager.OnHttpResponseListener;
import zuo.biao.library.ui.DatePickerWindow;
import zuo.biao.library.util.JSON;
import zuo.biao.library.util.TimeUtil;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.horizon.mobile.R;
import com.horizon.mobile.manage.SharePreferences;
import com.horizon.mobile.model.Result;
import com.horizon.mobile.searchdialog.SearchAct;
import com.horizon.mobile.util.HttpRequest;

/**
 * 代办设置界面
 */
public class AgentSetActivity extends BaseActivity implements
		OnBottomDragListener {
	private int[] selectedDate = new int[] { 1971, 00, 01 };
	ImageView configIV;
	EditText userNameEt;
	EditText beginDateEt;
	EditText endDateEt;
	String ids = "";
	String agentId = "";
	String agentName = "";
	CheckBox msgCb, smsCB, imCb, mailCb;
	RadioGroup radioGroup;
	SharePreferences sPreferences;
	Button sureBtn,cannelBtn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_agent_set, this);

		intent = getIntent();
		ids = intent.getStringExtra("ids");
		sPreferences = SharePreferences.getInstance(context);

		// 功能归类分区方法，必须调用<<<<<<<<<<
		initView();
		initData();
		initEvent();
		// 功能归类分区方法，必须调用>>>>>>>>>>
	}

	public static Intent createIntent(Context context, String ids) {
		return new Intent(context, AgentSetActivity.class).putExtra("ids", ids);
	}

	@Override
	public Activity getActivity() {
		// TODO Auto-generated method stub
		return this;
	}

	@Override
	public void initView() {
		userNameEt = (EditText) findViewById(R.id.userNameEt);
		beginDateEt = (EditText) findViewById(R.id.beginDateEt);
		endDateEt = (EditText) findViewById(R.id.endDateEt);
		msgCb = (CheckBox) findViewById(R.id.msgCb);
		smsCB = (CheckBox) findViewById(R.id.smsCB);
		imCb = (CheckBox) findViewById(R.id.imCb);
		mailCb = (CheckBox) findViewById(R.id.mailCb);
		radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
		sureBtn = (Button) findViewById(R.id.sureBtn);
		cannelBtn= (Button) findViewById(R.id.cannelBtn);

	}

	@Override
	public void initData() {
		// TODO Auto-generated method stub
		// tvBaseTitle.setText(ids.toString());
	}

	@Override
	public void initEvent() {
		cannelBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		sureBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if(agentId.length()==0){
					showShortToast("请选择代办人");
					return;
				}
				if(beginDateEt.getText().toString().length()==0){
					showShortToast("请选择开始时间");
					return;
				}
				if(endDateEt.getText().toString().length()==0){
					showShortToast("请选择结束时间");
					return;
				}
				String noticeType="";
				if(msgCb.isChecked()){
					noticeType+="Msg";
				}
				if(smsCB.isChecked()){
					if(noticeType.length()>0)
						noticeType+=","	;
					noticeType+="SMS";
				}
				if(imCb.isChecked()){
					if(noticeType.length()>0)
						noticeType+=","	;
					noticeType+="IM";
				}
				if(mailCb.isChecked()){
					if(noticeType.length()>0)
						noticeType+=","	;
					noticeType+="Mail";
				}
				 RadioButton rb = (RadioButton) findViewById(radioGroup.getCheckedRadioButtonId());
				 String moveTodo="";
				 if(rb.getText().toString().equals("是"))
					 moveTodo="1";
				 else
					 moveTodo="0";
				 HttpRequest.agentOperate(sPreferences.getToken(),ids,agentId,agentName,beginDateEt.getText().toString(),endDateEt.getText().toString(),moveTodo,noticeType,0,setListener);
			}
		});
		userNameEt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(AgentSetActivity.this,
						SearchAct.class);
				startActivityForResult(intent, 11);

			}
		});
		beginDateEt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				toActivity(DatePickerWindow.createIntent(context, new int[] {
						1971, 0, 1 },
						TimeUtil.getDateDetail(System.currentTimeMillis())),
						33, false);
			}
		});
		beginDateEt.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View arg0, boolean arg1) {
				// TODO Auto-generated method stub
				if (arg1) {
					toActivity(
							DatePickerWindow.createIntent(context, new int[] {
									1971, 0, 1 }, TimeUtil.getDateDetail(System
									.currentTimeMillis())), 33, false);
				}
			}
		});
		endDateEt.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View arg0, boolean arg1) {
				// TODO Auto-generated method stub
				if (arg1) {
					toActivity(
							DatePickerWindow.createIntent(context, new int[] {
									1971, 0, 1 }, TimeUtil.getDateDetail(System
									.currentTimeMillis())), 44, false);
				}
			}
		});
		endDateEt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				toActivity(DatePickerWindow.createIntent(context, new int[] {
						1971, 0, 1 },
						TimeUtil.getDateDetail(System.currentTimeMillis())),
						44, false);
			}
		});
	}

	@Override
	public void onDragBottom(boolean rightToLeft) {
		// TODO Auto-generated method stub
		finish();
	}

	OnHttpResponseListener setListener = new OnHttpResponseListener() {

		@Override
		public void onHttpResponse(int requestCode, String resultJson,
				Exception e) {
			// TODO Auto-generated method stub
			boolean isNull = resultJson == null;
			if (resultJson != null) {
				Result result = JSON.parseObject(resultJson, Result.class);
				if (result != null && result.isSuccess()) {
					showShortToast(result.getMessage());
					finish();
				} else {
					if (result != null)
						showShortToast(result.getMessage());
					else
						showShortToast("连接服务器失败！");
				}
			} else {
				if (e != null)
					showShortToast(e.getMessage());
				else
					showShortToast("连接服务器失败！");
			}

		}
	};

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode != RESULT_OK) {
			return;
		}
		switch (requestCode) {
		case 11:
			if (data != null) {
				agentId = data.getStringExtra("userId");
				agentName = data.getStringExtra("userName");
				userNameEt.setText(agentName);
			}
			break;
		case 33:
			if (data != null) {
				ArrayList<Integer> list = data
						.getIntegerArrayListExtra(DatePickerWindow.RESULT_DATE_DETAIL_LIST);
				if (list != null && list.size() >= 3) {

					selectedDate = new int[list.size()];
					for (int i = 0; i < list.size(); i++) {
						selectedDate[i] = list.get(i);
					}
					String month="";
					String day="";
					if((selectedDate[1] + 1)<10){
						month="0"+(selectedDate[1] + 1);
					}else {
						month = (selectedDate[1] + 1)+"" ;
					}
					if(selectedDate[2]<10){
						day = "0"+selectedDate[2];
					}else {
						day = selectedDate[2]+"";
					}
					beginDateEt.setText(selectedDate[0] + "-"
							+ month + "-" + day);
				}
			}
			break;
		case 44:
			if (data != null) {
				ArrayList<Integer> list = data
						.getIntegerArrayListExtra(DatePickerWindow.RESULT_DATE_DETAIL_LIST);
				if (list != null && list.size() >= 3) {

					selectedDate = new int[list.size()];
					for (int i = 0; i < list.size(); i++) {
						selectedDate[i] = list.get(i);
					}
					String month="";
					String day="";
					if((selectedDate[1] + 1)<10){
						month="0"+(selectedDate[1] + 1);
					}else {
						month = (selectedDate[1] + 1)+"" ;
					}
					if(selectedDate[2]<10){
						day = "0"+selectedDate[2];
					}else {
						day = selectedDate[2]+"";
					}
						
					endDateEt.setText(selectedDate[0] + "-"
							+  month  + "-" + day);
				}
			}
			break;
		}
	}
}
